<?php

namespace SaaS\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use SaaS\Models\Tenant;

class SaaSSchedule extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'saas:schedule';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '执行每个实例的计划任务';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle(): int
    {
        $tenants = Tenant::all();
        foreach ($tenants as $tenant) {
            $tenant_id = $tenant->id;
            Artisan::call('tenants:run --tenants=' . $tenant_id . ' schedule:run');
        }

        return 0;
    }
}
